package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.ruoyi.system.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DemandsMapper;
import com.ruoyi.system.mapper.QqMapper;
import com.ruoyi.system.mapper.StayTaskMapper;
import com.ruoyi.system.mapper.ClientMapper;
import com.ruoyi.system.mapper.QqChannelMapper;


import com.ruoyi.system.service.IDemandsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求 服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-28
 */
@Service
public class DemandsServiceImpl implements IDemandsService 
{
	@Autowired
	private DemandsMapper demandsMapper;

	@Autowired
	private QqMapper qqMapper;

	@Autowired
	private ClientMapper clientMapper;

	@Autowired
	private StayTaskMapper stayTaskMapper;

	@Autowired
	private QqChannelMapper qqChannelMapper ;
	/**
     * 查询每日需求，每个渠道的每个游戏的每日新增、付费和留存要求信息
     * 
     * @param id 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求ID
     * @return 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求信息
     */
    @Override
	public Demands selectDemandsById(Integer id)
	{
	    return demandsMapper.selectDemandsById(id);
	}
	
	/**
     * 查询每日需求，每个渠道的每个游戏的每日新增、付费和留存要求列表
     * 
     * @param demands 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求信息
     * @return 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求集合
     */
	@Override
	public List<Demands> selectDemandsList(Demands demands)
	{
	    return demandsMapper.selectDemandsList(demands);
	}
	
    /**
     * 新增每日需求，每个渠道的每个游戏的每日新增、付费和留存要求
     * 
     * @param demands 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求信息
     * @return 结果
     */
	@Override
	public int insertDemands(Demands demands)
	{
	    return demandsMapper.insertDemands(demands);
	}
	
	/**
     * 修改每日需求，每个渠道的每个游戏的每日新增、付费和留存要求
     * 
     * @param demands 每日需求，每个渠道的每个游戏的每日新增、付费和留存要求信息
     * @return 结果
     */
	@Override
	public int updateDemands(Demands demands)
	{
	    return demandsMapper.updateDemands(demands);
	}

	/**
     * 删除每日需求，每个渠道的每个游戏的每日新增、付费和留存要求对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteDemandsByIds(String ids)
	{
		return demandsMapper.deleteDemandsByIds(Convert.toStrArray(ids));
	}

	@Override
	public int generateRegisterTask(Integer id) {


		Demands demands = demandsMapper.selectDemandsById(id);

		Map<String,Object> map = new HashMap<>();
		map.put("gameId",demands.getChannel().getGameId());
		map.put("size",demands.getRegisterNum());

		ArrayList<Client>  clients = clientMapper.selectClientByGame(demands.getChannel().getGameId());
		ArrayList<Qq> qq = qqMapper.selectUnusedQq(map);

		int success = 0;
		int csize = clients.size();
		int qsize = qq.size();

		int each = qsize / csize;
		int leave = qsize % csize;

		for (int i=0;i<csize;i++){

			for(int j=0;j<each;j++){
				int ind = i*each+j;

				StayTask task = new StayTask();
				task.setChannelId(demands.getChannelId());
				task.setClientId(clients.get(i).getClientId());
				task.setQqId(qq.get(ind).getId());
				int flag = stayTaskMapper.insertRegisterTask(task);
				if(flag >0){
					QqChannel qqChannel  =new QqChannel();

					qqChannel.setChannelId(demands.getChannelId());
					qqChannel.setQqId(qq.get(ind).getId());

					qqChannelMapper.insertQqChannel(qqChannel);
					success +=1;

				}
			}
		}

		for(int n=0;n<leave;n++){

			StayTask task = new StayTask();
			task.setChannelId(demands.getChannelId());
			task.setClientId(clients.get(0).getClientId());
			task.setQqId(qq.get(qsize-1-n).getId());
			int flag = stayTaskMapper.insertRegisterTask(task);
			if(flag >0){
				QqChannel qqChannel  =new QqChannel();

				qqChannel.setChannelId(demands.getChannelId());
				qqChannel.setQqId(qq.get(qsize-1-n).getId());

				qqChannelMapper.insertQqChannel(qqChannel);

				success +=1;

			}
		}

		if(success == qq.size())
			return 1;
		else
			return -1;
	}

	@Override
	public int generateStayTask(Integer id) {

		Demands demands = demandsMapper.selectDemandsById(id);

		Date date=new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(calendar.DATE, -1);
		date = calendar.getTime();

		SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
		String dateString = format.format(date);


		Map<String,Object> map = new HashMap<>();

		map.put("beginTime",dateString);
		map.put("channelId",demands.getChannelId());


		int curday = stayTaskMapper.selectYStayTask(map);
		if(curday >0){
			String[] rates = demands.getStayRate().split(",");

			int size = Integer.valueOf(rates[curday-1]);

			size = demands.getLoginNum()*size;

			map.put("size",size);
			map.put("requireDays",demands.getStayDays());
			return  stayTaskMapper.createStayTask(map);
		}


		return -1;





	}
}
